<style lang="stylus" scoped>

</style>

<template>
  <div>
    <q-table
      class="bg-white shadow-1"
      :data="data"
      :columns="columns"
      row-key="id"
      separator="cell"
      selection="multiple"
      :selected.sync="selected"
      :visible-columns="visibleColumns"
      :filter="searchForm"
      :pagination.sync="pagination"
      :rows-per-page-options="[3, 5, 10, 20]"
      no-data-label="暂无数据"
      table-class="h-custom"
      @request="listGet"
    >
      <template v-slot:top-left="props">
        <q-btn dense color="light-blue" :label="$t('创建费率')" @click="add"/>
        <!-- <q-btn color="negative" flat :label="$t('批量删除')" class="q-ml-sm"/> -->
      </template>
      <template v-slot:top-right="props">
        <q-select
          v-model="visibleColumns"
          multiple
          borderless
          options-dense
          :display-value="$t('显示列')"
          emit-value
          map-options
          :options="columns"
          option-value="name"
          class="q-mx-lg"
        >
          <template v-slot:option="scope">
            <q-item
              v-bind="scope.itemProps"
              v-on="scope.itemEvents"
              v-if="!scope.opt.required"
            >
              <q-item-section avatar>
                <q-toggle color="blue" v-model="visibleColumns" :val="scope.opt.name" />
              </q-item-section>
              <q-item-section style="min-width:60px">
                {{ scope.opt.label }}
              </q-item-section>
            </q-item>
          </template>
        </q-select>
        <q-toggle
          v-model="searchToggle"
          left-label
          label="搜索"
          color="blue"
          checked-icon="check"
          unchecked-icon="clear"
          keep-color
        />
        <q-btn
          flat round dense
          :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'"
          @click="props.toggleFullscreen"
          class="q-ml-md"
        />
      </template>
      <template v-if="searchToggle" v-slot:top-row="props">
        <q-tr :props="props">
          <q-td>
          </q-td>
          <q-td v-for="col in props.cols" :key="col.name" class="relative-position">
            <q-btn color="primary" dense flat icon="search" class="absolute-center" v-if="col.name==='action'" @click="listGet({ pagination: pagination, filter: searchForm })"/>
            <q-input dense outlined v-model="searchForm[col.name]" v-else/>
          </q-td>
        </q-tr>
      </template>
      <template v-slot:header="props">
        <q-tr :props="props">
          <q-th>
            <q-checkbox indeterminate-value="some" dense v-model="props.selected" />
          </q-th>
          <q-th v-for="col in props.cols" :key="col.name" :props="props">
            {{ col.label }}
          </q-th>
        </q-tr>
      </template>
      <template v-slot:body="props">
        <q-tr :props="props">
          <q-td width="52">
            <q-checkbox dense v-model="props.selected" />
          </q-td>
          <q-td key="action" :props="props">
            <q-btn-dropdown
              color="blue"
              size="sm"
              dense
              menu-anchor="bottom left"
              menu-self="top left"
              content-class="text-grey"
              icon="settings">
              <q-list dense>
                <q-item clickable v-close-popup @click="edit(props.row)">
                  <q-item-section>
                    <q-item-label>修改</q-item-label>
                  </q-item-section>
                </q-item>
                <q-item clickable v-close-popup @click="delRow(props.row)">
                  <q-item-section>
                    <q-item-label>删除</q-item-label>
                  </q-item-section>
                </q-item>
                <q-item clickable v-close-popup @click="props.expand = !props.expand">
                  <q-item-section>
                    <q-item-label>{{ props.expand? '收起': '更多字段' }}</q-item-label>
                  </q-item-section>
                </q-item>
              </q-list>
            </q-btn-dropdown>
          </q-td>
          <q-td key="code" :props="props">{{ props.row.code }}</q-td>
          <q-td key="name" :props="props">{{ props.row.name  }}</q-td>
          <q-td key="nameEn" :props="props">{{ props.row.nameEn  }}</q-td>
          <q-td key="active" :props="props">
            <q-chip v-if="props.row.active == '0'" color="light">禁用</q-chip>
            <q-chip v-if="props.row.active == '1'" color="positive">启用</q-chip>
          </q-td>
          <q-td key="payRate" :props="props">{{ props.row.payRate  }}</q-td>
          <q-td key="billRate" :props="props">{{ props.row.billRate  }}</q-td>
          <q-td key="billUnitName" :props="props">{{ props.row.billUnitName  }}</q-td>
          <q-td key="currentCode" :props="props">{{ props.row.currentCode  }}</q-td>
          <q-td key="currentName" :props="props">{{ props.row.currentName  }}</q-td>
          <q-td key="createTime" :props="props">{{ props.row.createTime  }}</q-td>
          <q-td key="createUserName" :props="props">{{ props.row.createUserName }}</q-td>
          <q-td key="remark" :props="props">{{ props.row.remark  }}</q-td>
        </q-tr>
        <q-tr v-show="props.expand" :props="props">
          <q-td colspan="100%">
          </q-td>
        </q-tr>
      </template>
      <template v-slot:pagination="props">
        <q-pagination
          v-model="pagination.page"
          color="blue"
          :max-pages="5"
          :max="pages"
          :boundaryLinks="true"
          :directionLinks="true"
        />
        共 {{ total }} 条
      </template>
    </q-table>
    <q-dialog
      v-model="addOrEditDialog"
      persistent
      :maximized="maximizedToggle"
      transition-show="slide-up"
      transition-hide="slide-down">
      <q-card class="bg-white relative-position" style="min-width: 40vw">
        <q-bar class="bg-white q-py-lg">
          <strong>{{ addOrEdit === 1? '新增' : '修改'}}</strong>
          <q-space />
          <q-btn dense flat icon="minimize" @click="maximizedToggle = false" :disable="!maximizedToggle">
            <q-tooltip v-if="maximizedToggle" content-class="bg-white text-primary">最小化</q-tooltip>
          </q-btn>
          <q-btn dense flat icon="crop_square" @click="maximizedToggle = true" :disable="maximizedToggle">
            <q-tooltip v-if="!maximizedToggle" content-class="bg-white text-primary">最大化</q-tooltip>
          </q-btn>
          <q-btn dense flat icon="close" @click="addOrEditDialog = false">
            <q-tooltip content-class="bg-white text-primary">关闭</q-tooltip>
          </q-btn>
        </q-bar>
        <q-separator />
        <q-card-section :style="maximizedToggle?'margin-bottom:53px;max-height:83.5vh': 'margin-bottom:53px;min-height:50vh;max-height:60vh'" class="scroll">
          <div class="row q-col-gutter-sm">
            <div class="col-12">
              <div :class="$v.addOrEditForm.name.$error?'text-negative': ''">名称</div>
              <q-input
                v-model="addOrEditForm.name"
                error-message="该字段为必填项"
                :error="$v.addOrEditForm.name.$error"
                @blur="$v.addOrEditForm.name.$touch"
                dense hint/>
            </div>
            <div class="col-12">
              <div>英文名称</div>
              <q-input
                v-model="addOrEditForm.nameEn"
                dense hint/>
            </div>
            <div class="col-12"  style="padding-bottom:20px;">
              <div>交付类型</div>
                <q-select
                  :options="deliver"
                  v-model="addOrEditForm.payTypeId"
                  emit-value
                  map-options
                  dense></q-select>
            </div>
            <div class="col-12"  style="padding-bottom:20px;">
              <div>工资标准</div>
              <q-input
                type="number"
                v-model="addOrEditForm.payRate"
                dense hint/>
            </div>
            <div class="col-12"  style="padding-bottom:20px;">
              <div>工资标准</div>
                <q-select
                  :options="settlement"
                  v-model="addOrEditForm.payUnitId"
                  emit-value
                  map-options
                  dense></q-select>
            </div>
            <div class="col-12"  style="padding-bottom:20px;">
              <div>结算费率单位</div>
                <q-select
                  :options="settlement"
                  v-model="addOrEditForm.billUnitId"
                  emit-value
                  map-options
                  dense></q-select>
            </div>
            <div class="col-12"  style="padding-bottom:20px;">
              <div>结算费率</div>
              <q-input
                type="number"
                v-model="addOrEditForm.billRate"
                dense hint/>
            </div>
            <div class="col-12"  style="padding-bottom:20px;">
              <div>结算类型</div>
                <q-select
                  :options="type"
                  v-model="addOrEditForm.rateType"
                  emit-value
                  map-options
                  dense></q-select>
            </div>
            <div class="col-12"  style="padding-bottom:20px;">
              <div>选择币种</div>
                <q-select
                  :options="typeOption"
                  v-model="addOrEditForm.currentId"
                  emit-value
                  map-options
                  dense></q-select>
            </div>
            <div class="col-12" style="padding-bottom:20px;">
              <div :class="$v.addOrEditForm.name.$error?'text-negative': ''">是否启用</div>
                <q-select
                  :options="typeOpti"
                  v-model="addOrEditForm.active"
                  emit-value
                  map-options
                  dense></q-select>
            </div>
            <div class="col-12">
              <div>说明</div>
              <q-input
                maxlength="200"
                type="textarea"
                v-model="addOrEditForm.remark"
                dense hint/>
            </div>
          </div>
        </q-card-section>
        <q-separator class="absolute" style="bottom: 52px; left: 0"/>
        <q-card-actions align="right" class="absolute-bottom bg-white">
          <q-btn flat label="取消" color="primary" v-close-popup />
          <q-btn flat label="保存" color="primary" :loading="loadingSave" @click="saveHandler" />
        </q-card-actions>
      </q-card>
    </q-dialog>
  </div>
</template>

<script>
import { required } from 'vuelidate/lib/validators'
export default {
  name: 'Reate',
  data () {
    return {
      searchToggle: false,
      searchForm: {
        code: '',
        name: '',
        nameEn: '',
        active: '',
        payRate: '',
        billRate: '',
        billUnitName: '',
        currentCode: '',
        currentName: '',
        createTime: '',
        createUserName: '',
        remark: ''
      },
      typeOpti: [
        {
          label: '否',
          value: 0
        },
        {
          label: '是',
          value: 1
        }
      ],
      // 结算类型
      type: [],
      // 选择币种
      typeOption: [],
      // 交付类型
      deliver: [],
      selected: [],
      visibleColumns: [
        'action',
        'code',
        'name',
        'nameEn',
        'active',
        'payRate',
        'billRate',
        'billUnitName',
        'currentCode',
        'currentName',
        'createTime',
        'createUserName',
        'remark'
      ],
      // 当加入 required: true 属性时  visibleColumns 对当前字段无效
      columns: [
        { name: 'action', align: 'center', label: '操作' },
        { name: 'code', required: true, label: '编码', align: 'left', field: 'code', sortable: true },
        { name: 'name', required: true, label: '名称', align: 'left', field: 'name', sortable: true },
        { name: 'nameEn', required: false, label: '英文名称', align: 'left', field: 'nameEn', sortable: true },
        { name: 'active', required: false, label: '是否启用', align: 'left', field: 'active', sortable: true },
        { name: 'payRate', required: true, label: '工资标准', align: 'left', field: 'payRate', sortable: true },
        { name: 'billRate', required: true, label: '结算费率', align: 'left', field: 'billRate', sortable: true },
        { name: 'billUnitName', required: true, label: '结算费率单位名称', align: 'left', field: 'billUnitName', sortable: true },
        { name: 'currentCode', required: false, label: '货币编码', align: 'left', field: 'currentCode', sortable: true },
        { name: 'currentName', required: false, label: '货币名称', align: 'left', field: 'currentName', sortable: true },
        { name: 'createTime', field: 'createTime', align: 'left', label: '创建时间', sortable: true },
        { name: 'createUserName', field: 'createUserName', align: 'left', label: '创建人', sortable: true },
        { name: 'remark', field: 'remark', align: 'left', label: '说明', sortable: false }
      ],
      data: [],
      total: 0,
      pagination: {
        page: 1,
        rowsPerPage: 10,
        rowsNumber: 10,
        sortBy: '',
        descending: Boolean
      },
      // dialog
      addOrEdit: 1, // 1 新增  2 修改
      loadingSave: false,
      addOrEditDialog: false,
      maximizedToggle: false,
      addOrEditTab: 'tab1',
      addOrEditForm: {
        rateType: '',
        name: '',
        nameEn: '',
        currentId: '',
        billRate: '',
        payUnitId: '',
        payRate: '',
        payTypeId: '',
        active: 1,
        billUnitId: '',
        remark: ''
      },
      // 获取结算类型
      settlement: []
    }
  },
  computed: {
    pages () {
      return Math.ceil(this.total / this.pagination.rowsPerPage)
    }
  },
  validations: {
    addOrEditForm: {
      name: { required }
    }
  },
  watch: {
    'pagination.page' () {
      this.listGet({
        pagination: this.pagination,
        filter: this.searchForm
      })
    }
  },
  mounted () {
    this.init()
    this.currencyDataGet()
    this.currencyDeliver()
    this.currencySettlement()
    this.currencyType()
  },
  methods: {
    init () {
      this.listGet({
        pagination: this.pagination,
        filter: this.searchForm
      })
    },
    // 获取交付类型
    async currencyDeliver () {
      await this.$api({
        method: 'post',
        url: '/common/dict/findByTypeCode/10',
        emulateJSON: true,
        useLoadLayer: true,
        successCallback: ({ data }) => {
          console.log(data)
          let arr = []
          data.forEach(item => {
            let o = {}
            o.label = item.name
            o.value = item.id
            arr.push(o)
          })
          console.log(arr)
          this.deliver = arr
        }
      })
    },
    // 结算类型
    async currencyType () {
      await this.$api({
        method: 'post',
        url: '/common/dict/findByTypeCode/10',
        emulateJSON: true,
        useLoadLayer: true,
        successCallback: ({ data }) => {
          console.log(data)
          let arr = []
          data.forEach(item => {
            let o = {}
            o.label = item.name
            o.value = item.id
            arr.push(o)
          })
          console.log(arr)
          this.type = arr
        }
      })
    },
    // 获取结算类型
    async currencySettlement () {
      await this.$api({
        method: 'post',
        url: '/common/unit/selectByType/1',
        emulateJSON: true,
        useLoadLayer: true,
        successCallback: ({ data }) => {
          console.log(data)
          let arr = []
          data.forEach(item => {
            let o = {}
            o.label = item.name
            o.value = item.id
            arr.push(o)
          })
          console.log(arr)
          this.settlement = arr
        }
      })
    },
    // 获取币种
    async currencyDataGet () {
      await this.$api({
        method: 'post',
        url: '/common/currency/selectAll',
        successCallback: ({ data }) => {
          console.log(data)
          let arr = []
          data.forEach(item => {
            let o = {}
            o.label = item.name
            o.value = item.id
            arr.push(o)
          })
          console.log(arr)
          this.typeOption = arr
        }
      })
    },
    async listGet (props) {
      let { page, rowsPerPage, sortBy, descending } = props.pagination, filter = props.filter
      await this.$api({
        method: 'post',
        url: '/mdm/work/rate/query',
        params: {
          pageNo: page,
          pageSize: rowsPerPage,
          sort: sortBy,
          order: descending === true ? 'desc' : (descending === false ? 'asc' : ''),
          ...filter
        },
        emulateJSON: true,
        useLoadLayer: true,
        successCallback: ({ data }) => {
          this.total = data.total
          this.data = data.list
        },
        errorCallback (err) {
          console.log('未处理的业务级error：', err)
        }
      })
      this.pagination.page = page
      this.pagination.rowsPerPage = rowsPerPage
      this.pagination.sortBy = sortBy
      this.pagination.descending = descending
    },
    add () {
      this.addOrEdit = 1
      this.addOrEditTab = 'tab1'
      this.addOrEditForm = {
        code: '',
        nameEn: '',
        currentId: '',
        payTypeId: '',
        active: 1,
        billUnitId: '',
        billRate: '',
        payUnitId: '',
        payRate: '',
        remark: '',
        rateType: ''
      }
      this.$v.addOrEditForm.$reset()
      this.addOrEditDialog = true
    },
    async edit (row) {
      await this.$api({
        method: 'get',
        url: `/mdm/work/rate/findById/${row.id}`,
        useLoadLayer: true,
        successCallback: ({ data }) => {
          this.addOrEditDialog = true
          this.addOrEditForm = { ...data }
        },
        errorCallback (err) {
          console.log('未处理的业务级error：', err)
        }
      })
      this.addOrEdit = 2
      this.addOrEditTab = 'tab1'
      this.$v.addOrEditForm.$reset()
    },
    async saveHandler () {
      this.$v.addOrEditForm.$touch()
      if (!this.$v.addOrEditForm.$error) {
        console.log('46546546', this.addOrEdit)
        this.loadingSave = true
        await this.$api({
          method: 'put',
          url: this.addOrEdit === 1 ? '/mdm/work/rate/add' : '/mdm/work/rate/update',
          params: this.addOrEditForm,
          emulateJSON: true,
          useNotify: true,
          successCallback: ({ data }) => {
            this.addOrEditDialog = false
          },
          errorCallback (err) {
            console.log('未处理的业务级error：', err)
          }
        })
        this.loadingSave = false
        this.listGet({
          pagination: this.pagination,
          filter: this.searchForm
        })
      }
    },
    delRow (row) {
      this.$q.dialog({
        title: '删除',
        message: `确认要删除费率：${row.name} 吗？`,
        ok: true,
        cancel: true,
        persistent: true
      }).onOk(async () => {
        await this.$api({
          method: 'delete',
          url: `/mdm/work/rate/delete/${row.id}`,
          useLoadLayer: true,
          useNotify: true,
          successCallback: ({ data }) => {
            console.log(data)
          },
          errorCallback (err) {
            console.log('未处理的业务级error：', err)
          }
        })
        this.listGet({
          pagination: this.pagination,
          filter: this.searchForm
        })
      }).onCancel(() => {

      })
    }
  }
}
</script>
